<script lang="ts" setup>
import type { SystemUserApi } from '#/api/system/user';

import { computed, ref, watch } from 'vue';

import { useVbenModal } from '@vben/common-ui';
import { useUserStore } from '@vben/stores';

import { Checkbox, CheckboxGroup, message } from 'ant-design-vue';

import { getUser, updateUserSystemPermissions } from '#/api/system/user';
import { $t } from '#/locales';

const emit = defineEmits(['success']);
const userStore = useUserStore();
const formData = ref<{ id: number | undefined; systemPermissions: string[] }>({
  id: undefined,
  systemPermissions: [],
});
const permissionDictOption = computed(() => {
  const isSupAdmin = userStore.userInfo?.isSupAdmin || false;
  let options = [
    { label: '超级管理员', value: 'SUPERADMIN' },
    // { label: '所有菜单可见', value: 'ALLSCENE' },
    // { label: '可操作所有功能', value: 'ALLFUNCTION' },
    // { label: '可操作所有审核', value: 'ALLAudit' },
    // { label: '所有区域（组织）权限', value: 'ALLORG' },
    { label: '用户授权区域权限', value: 'AREA' },
    { label: '用户授权区域及以下权限', value: 'AREACHILD' },
    { label: '用户授权单位权限', value: 'UNIT' },
    { label: '用户授权单位及以下权限', value: 'UNITCHILD' },
    { label: '用户授权部门权限', value: 'DEPT' },
    { label: '用户授权部门及以下权限', value: 'DEPTCHILD' },
    { label: '所有流程节点权限', value: 'ALLFLOWNODE' },
    { label: '所有查询', value: 'ALLQUERY' },
    { label: '所有编辑包括增删改', value: 'ALLEDIT' },
    { label: '所有新增', value: 'ALLINSERT' },
    { label: '所有修改', value: 'ALLUPDATE' },
    { label: '所有删除', value: 'ALLDELETE' },
    { label: '所有数据导入', value: 'IMP' },
    { label: '所有数据导出', value: 'EXP' },
    { label: '所有打印', value: 'PRINT' },
  ];
  if (isSupAdmin) {
    return options;
  } else {
    options = options.filter((item) => item.value !== 'SUPERADMIN');
    return options;
  }
});
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    modalApi.lock();
    // 提交表单
    try {
      await updateUserSystemPermissions(
        formData.value.id,
        formData.value.systemPermissions,
      );
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = { id: undefined, systemPermissions: [] };
      return;
    }
    // 加载数据
    const data = modalApi.getData<SystemUserApi.User>();
    if (!data || !data.id) {
      return;
    }
    try {
      const userData = await getUser(data.id as number);
      const myData = {
        id: data.id,
        systemPermissions: userData.systemPermissions || [],
      };
      formData.value = myData;
    } finally {
      modalApi.unlock();
    }
  },
});
// 监听 formData 更新以调试
watch(
  () => formData.value.systemPermissions,
  (newVal) => {
    console.log('systemPermissions updated:', newVal);
  },
);
</script>

<template>
  <Modal title="系统级授权" class="w-1/5">
    <CheckboxGroup
      v-model:value="formData.systemPermissions"
      class="flex flex-col gap-2"
    >
      <Checkbox
        v-for="(dict, index) in permissionDictOption"
        :key="index"
        :value="dict.value"
      >
        {{ dict.label }}
      </Checkbox>
    </CheckboxGroup>
  </Modal>
</template>
